﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using admin.ServiceReference1;
using System.IO;
using System.Windows.Browser;

namespace admin.two
{
    public partial class ads : UserControl
    {
        Service1Client sc;
        bytedata streamdata = new bytedata();

        public ads()
        {
            InitializeComponent();
            bt_add.Click += new RoutedEventHandler(bt_add_Click);
            bt_cancel.Click += new RoutedEventHandler(bt_cancel_Click);
            bt_selectfile.Click += new RoutedEventHandler(bt_selectfile_Click);
            bt_post.Click += new RoutedEventHandler(bt_post_Click);
            this.Loaded += new RoutedEventHandler(ads_Loaded);
            this.bt_del.Click += new RoutedEventHandler(bt_del_Click);
        }

        void bt_del_Click(object sender, RoutedEventArgs e)
        {
            if (gridview.SelectedItems.Count == 1)
            {
                ad data = gridview.SelectedItem as ad;
                sc.deleteAsync(data.id);
            }
        }

        void ads_Loaded(object sender, RoutedEventArgs e)
        {
            sc = serverManager.getPox();
            sc.getADCompleted += new EventHandler<getADCompletedEventArgs>(sc_getADCompleted);
            sc.getADAsync();
            gridview.SelectionChanged +=new SelectionChangedEventHandler(gridview_SelectionChanged);
            sc.deleteCompleted += new EventHandler<deleteCompletedEventArgs>(sc_deleteCompleted);
        }

        void sc_deleteCompleted(object sender, deleteCompletedEventArgs e)
        {
            bool sum = e.Result;
            if (sum)
            {
                HtmlPage.Window.Alert("删除成功");
            }
            else
            {
                HtmlPage.Window.Alert("操作失败");
            }
            sc.getADAsync();
        }

        void gridview_SelectionChanged(object sender, EventArgs e)
        {
            if (gridview.SelectedItems.Count == 1)
            {
                bt_del.IsEnabled = true;
            }
            else
            {
                bt_del.IsEnabled = false;
            }
        }

        void sc_getADCompleted(object sender, getADCompletedEventArgs e)
        {
            List<ad> data = new List<ad>(e.Result);
            gridview.ItemsSource = data;
        }

        void bt_post_Click(object sender, RoutedEventArgs e)
        {
            ad newad = new ad();
            newad.sort = lb_srot.SelectedIndex;
            newad.title = tb_title.Text;
            if (newad.sort == 0)
            {
                newad.img = tb_filename.Text;
            }
            else if (newad.sort == 1)
            {
                newad.video = tb_filename.Text;
            }
            newad.clickurl = tb_link.Text;

            sc.addADCompleted += new EventHandler<addADCompletedEventArgs>(sc_addADCompleted);
            sc.addADAsync(newad, streamdata);

            finishadd();

        }

        void finishadd()
        {
            bt_cancel_Click(this, new RoutedEventArgs());
            lb_srot.SelectedIndex = 0;
            tb_selectfilename.Text = string.Empty;
            tb_title.Text = string.Empty;
            tb_filename.Text = string.Empty;
            tb_link.Text = string.Empty;
            sc.getADAsync();
        }

        void sc_addADCompleted(object sender, addADCompletedEventArgs e)
        {
            bool re = (bool)e.Result;
            if (re)
            {
                HtmlPage.Window.Alert("添加成功");
            }
            else
            {
                HtmlPage.Window.Alert(e.Error.Message);
            }
        }

        void bt_selectfile_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            of.Filter = "图片文件(*.jpg;*.jpeg;*.png)|*.jpg;*.jpeg;*.png|视频文件(*.wmv)|*.wmv";
            of.Multiselect = false;
            if (of.ShowDialog() == true)
            {
                tb_filename.Text = tb_selectfilename.Text = of.File.Name;
                setstream(of.File.OpenRead());
            }
        }

        void bt_cancel_Click(object sender, RoutedEventArgs e)
        {
            VisualStateManager.GoToState(this, "endadd", true);
        }

        void bt_add_Click(object sender, RoutedEventArgs e)
        {
            VisualStateManager.GoToState(this, "add", true);
        }

        void setstream(Stream stream)
        {
            byte[] data = new byte[stream.Length];
            stream.Read(data, 0, data.Length);
            streamdata.filedata = data;
            data = null;
        }
    }
}
